package com.zbbt.sportslottery.ui.activity;

import java.util.ArrayList;
import java.util.List;
import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.HuoDongQuanData;
import com.zbbt.sportslottery.bean.SuperResult;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.adapter.HDQListAdapter;
import com.zbbt.sportslottery.ui.adapter.HDQListAdapter.OnBottomClickListener;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout.OnRefreshListener;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.ToastUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * 
 * @ClassName: HDQListActivity
 * @Description: 体验券列表
 * @author：GaomingShuo
 * @date：2015-3-5 下午3:07:43
 * @version V1.0
 */

public class HDQListActivity extends SimpleBaseActivity {

	private SwipeRefreshLayout mSwipeRefreshLayout;
	private LinearLayout imgNoExperience;
	private ListView mListView;

	private List<HuoDongQuanData> dataList = new ArrayList<HuoDongQuanData>();
	private HDQListAdapter listAdapter = null;
	private int curPage = 1;
	/** 当前第几页 */
	private int pageSize = 10;
	/** 每页多少条记录 */
	private boolean hasmore_data = true;// 是否有下一页数据
	private ProgressBarCircularIndetermininate progressBar;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_huodongquan);
	}

	@Override
	protected void initView() {
		initData();
		mListView = (ListView) findViewById(R.id.mListView);
		progressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBarCircularIndetermininate);
		progressBar.setVisibility(View.VISIBLE);
		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.mSwipeRefreshLayout);
		imgNoExperience = (LinearLayout) findViewById(R.id.img_no_experience);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				curPage = 1;
				hasmore_data = true;
				httpData(Constant.REFRESH);
			}
		});
		if (listAdapter == null) {
			listAdapter = new HDQListAdapter(context, dataList);
			listAdapter.setOnDialogClickListener(new HDQListAdapter.OnDialogClickListener() {
				@Override
				public void onButtonClick(final String code, final String all_str) {
					Intent intent = new Intent(context, HDQInfoActivity.class);
					intent.putExtra("dhm_code", code);
					intent.putExtra("all_str", all_str.toString());
					startActivity(intent);
				}
			});
			listAdapter.setOnBottomClickListener(new OnBottomClickListener() {

				@Override
				public void onBottomClick() {
					if (hasmore_data) {
						httpData(Constant.HAS_MORE);
					}
				}
			});
			mListView.setAdapter(listAdapter);
		}
		httpData(Constant.REFRESH);
	}

	private void httpData(final int type) {
		// TODO待完善
		String userId = SharePreferUtil.getString(Constant.USER_ID, "");
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("userId", userId);
		jsonObject.addProperty("curPage", curPage);
		jsonObject.addProperty("pageSize", pageSize);
		jsonObject.addProperty("couponFlag", 2);
		ZbHttpClient.postNewApi(ServerConfig.API_TIYANQUAN, jsonObject,
				new TypeGsonHttpHandler<SuperResult<HuoDongQuanData>>() {
					@SuppressWarnings("unused")
					@Override
					public void onSuccess(SuperResult<HuoDongQuanData> data) {
						ArrayList<HuoDongQuanData> huoDatas = data.getData();
						if (data != null) {
							if (type == Constant.REFRESH) {
								dataList.clear();
								dataList.addAll(huoDatas);
							} else if (type == Constant.HAS_MORE) {
								dataList.addAll(huoDatas);
							}
							if (dataList.size() == pageSize) {
								curPage++;
							} else {
								hasmore_data = false;
							}
							listAdapter.notifyDataSetChanged();
							mSwipeRefreshLayout.setRefreshing(false);
							imgNoExperience.setVisibility(View.GONE);
							progressBar.setVisibility(View.GONE);
						} else {
							progressBar.setVisibility(View.GONE);
							imgNoExperience.setVisibility(View.VISIBLE);
						}
					}

					@Override
					public void onFailure(String errorCode, String errorMsg) {
						ToastUtil.showLong(errorMsg);
						if (dataList.size() == 0) {
							imgNoExperience.setVisibility(View.VISIBLE);
						}
						progressBar.setVisibility(View.GONE);
						mSwipeRefreshLayout.setRefreshing(false);
					}
				});
	}

	@Override
	protected void setListener() {}

	@Override
	protected void onClickEvent(View v) {}

	@Override
	protected void initData() {}
}
